package com.hnust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnust.entity.Position;
import com.hnust.entity.SensorData;
import com.hnust.entity.dto.PositionDto;
import com.hnust.entity.dto.SensorDataDto;
import com.hnust.entity.dto.SensorDataDto1;
import com.hnust.mapper.SensorDataMapper;
import com.hnust.service.PositionService;
import com.hnust.service.SensorDataService;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 创建人：zhangRuWang
 * 创建时间：2022/5/5
 * 描述：
 */
@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorDataMapper, SensorData> implements SensorDataService {
    @Autowired
    private PositionService positionService;

    @Override
    public Page<SensorDataDto> sensorDataSearch(int page, int pageSize,
                                                String beginTime,String endTime,
                                                String position,String uid) {
        Page<SensorData> sensorDataPage=new Page<>(page,pageSize);
        Page<SensorDataDto> sensorDataDtoPage=new Page<>(page,pageSize);
        QueryWrapper<SensorData> queryWrapper=new QueryWrapper<SensorData>()
                .apply(beginTime!=null&&!beginTime.equals(""),"date_format (update_time,'%Y-%m-%d %H:%i:%s') >= date_format('"
                        + beginTime + "','%Y-%m-%d %H:%i:%s')")
                .apply(endTime!=null&&!endTime.equals(""),"date_format (update_time,'%Y-%m-%d %H:%i:%s') <= date_format('"
                        + endTime + "','%Y-%m-%d %H:%i:%s')")
                .orderByDesc("update_time");
        queryWrapper.eq(uid!=null,"uid",uid);
        if(position!=null){
            String[] positions=position.split(",");
            List<String> positionIds=positionService.positionCodeToList(
                    Integer.parseInt(positions[0]),Integer.parseInt(positions[1]),
                    Integer.parseInt(positions[2]),Integer.parseInt(positions[3])
            );
            queryWrapper.in("position_id",positionIds);
        }
        this.page(sensorDataPage,queryWrapper);
        BeanUtils.copyProperties(sensorDataPage,sensorDataDtoPage,"records");
        List<SensorData> sensorDataList=sensorDataPage.getRecords();
        List<SensorDataDto> sensorDataDtos=sensorDataList.stream().map((item)->{
            SensorDataDto s=new SensorDataDto();
            this.sensorDataToDto(s,item);
            return s;
        }).collect(Collectors.toList());
        sensorDataDtoPage.setRecords(sensorDataDtos);
        return sensorDataDtoPage;
    }

    public void sensorDataToDto(SensorDataDto sensorDataDto,SensorData sensorData){
        BeanUtils.copyProperties(sensorData,sensorDataDto);//把一些同名的属性复制到另一个对象中
        Position p=positionService.getById(sensorData.getPositionId());//通过sensorData中的位置id找到对应Position
        PositionDto positionDto=new PositionDto();//创建PositionDto
        positionService.positionToDto(positionDto,p);//将对应Position转化成PositionDto
        sensorDataDto.setPositionDto(positionDto);//将PositionDto存入sensorDataDto
    }

    public List<SensorData> sensorDataShow(String positionId) {
        QueryWrapper<SensorData> wrapper = new QueryWrapper<>();
        wrapper
                .eq("position_id",positionId)
        .orderByDesc("update_time");
        List<SensorData> sensorDatas = this.list(wrapper);
        return sensorDatas;
    }

}

